<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>新增销售订单</title>
    <!--{include file='comm/comm_region.tpl'}-->
</head>

<body>

<div class="panel panel-default">

    <div class="comm_right">
        <div class="comm_crub">
            <span class="now_crub">销售订单</span>
        </div>

        <div class="comm_content">

            <div class="panel-body">

                <div class="form-horizontal" style="position:relative">
                    <div class="form-group" <!--{if $type==1}-->style="display: none"<!--{/if}-->>
                        <label class="col-sm-2 control-label">
                            客户信息：
                        </label>

                        <div class="col-sm-3">
                            <input type="hidden" id="saleCode" value="<!--{$saleInfo->saleCode|default:''}-->">
                            <input type="hidden" id="companyId" value="<!--{$saleInfo->companyId|default:''}-->">
                            <input id="companyName" type="text" class="form-control" readonly="readonly" value="<!--{$saleInfo->company_name|default:''}-->">
                        </div>
                        <div class="col-sm-3">
                            <button type="button" class="btn btn-primary" onclick="searchCompany(1);">查询</button>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-2 control-label">
                            项目名称：
                        </label>
                        <div class="col-sm-3">
                            <input id="saleName" class="form-control" value="<!--{$saleInfo->saleName|default:''}-->"/>
                        </div>
                        <label class="col-sm-2 control-label">
                            施工面积：
                        </label>
                        <div class="col-sm-3">
                            <input id="area" class="form-control" value="<!--{$saleInfo->area|default:''}-->"/>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-2 control-label">
                            涂装体系：
                        </label>
                        <div class="col-sm-3">
                            <input id="categoryName" class="form-control" value="<!--{$saleInfo->categoryName|default:''}-->"/>
                        </div>
                        <label class="col-sm-2 control-label">
                            要求完成时间：
                        </label>
                        <div class="col-sm-3">
                            <input id="requestDate" class="form-control" value="<!--{$saleInfo->requestDate|default:''}-->" readonly="readonly" />
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-2 control-label">
                            制作内容及要求：
                        </label>
                        <div class="col-sm-3">
                            <textarea id="request" class="form-control" rows="3"><!--{$saleInfo->request|default:''}--></textarea>
                        </div>
                        <label class="col-sm-2 control-label">
                            物流方式：
                        </label>
                        <div class="col-sm-3">
                            <select class="form-control" id="expressId">
                                <option value="">请选择</option>
                                <!--{foreach from=$send_array item=send}-->
                                <!--{if $saleInfo && $saleInfo->expressId == $send['sendTypeID']}-->
                                <option value="<!--{$send['sendTypeID']}-->" selected><!--{$send['sendTypeName']}--></option>
                                <!--{else}-->
                                <option value="<!--{$send['sendTypeID']}-->"><!--{$send['sendTypeName']}--></option>
                                <!--{/if}-->
                                <!--{/foreach}-->
                            </select>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-2 control-label">
                            收货人：
                        </label>
                        <div class="col-sm-3">
                            <input id="receiver" class="form-control" value="<!--{$saleInfo->receiver|default:''}-->"/>
                        </div>
                        <label class="col-sm-2 control-label">
                            收货人电话：
                        </label>
                        <div class="col-sm-3">
                            <input id="receivMobile" class="form-control" value="<!--{$saleInfo->receivMobile|default:''}-->"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">
                            收货地址：
                        </label>
                        <div class="col-sm-10">
                            <select class="form-control fl" id="province" style="width: 100px;margin-right: 5px;">
                                <option value="">请选择...</option>
                            </select>
                            <select class="form-control fl" id="city" style="width: 100px;margin-right: 5px;">
                                <option value="">请选择...</option>
                            </select>
                            <select class="form-control fl" id="county" style="width: 100px;margin-right: 5px;">
                                <option value="">请选择...</option>
                            </select>
                            <input id="address" class="form-control fl w300"value="<!--{$saleInfo->address|default:''}-->" placeholder="详细地址"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">
                            备注：
                        </label>
                        <div class="col-sm-10">
                            <textarea id="remark" class="form-control w300" rows="3"><!--{$saleInfo->remark|default:''}--></textarea>
                        </div>
                    </div>
                    <div style="text-align: right; margin-bottom: 5px">
                        <input class="btn btn-primary btn-sm" type="button" onclick="addGoods()" value="添加"/>
                    </div>
                    <table class="table table-bordered table-hover">
                        <thead>
                        <tr>
                            <th width="12%" style="text-align:center;">产品系列</th>
                            <th width="12%" style="text-align:center;">材料型号品名</th>
                            <th width="9%" style="text-align:center;">颜色</th>
                            <th width="9%" style="text-align:center;">对色标准</th>
                            <th width="10%" style="text-align:center;">包装</th>
                            <th width="10%" style="text-align:center;">规格</th>
                            <th width="7%" style="text-align:center;">数量</th>
                            <th width="8%" style="text-align:center;<!--{if $type==1}-->display: none<!--{/if}-->">单价</th>
                            <th width="8%" style="text-align:center;<!--{if $type==1}-->display: none<!--{/if}-->">打浆费用</th>
                            <th width="7%" style="text-align:center;<!--{if $type==1}-->display: none<!--{/if}-->">小计</th>
                            <th width="8%" style="text-align:center;">操作</th>
                        </tr>
                        </thead>
                        <tbody id="goodsList">
                            <!--{if $saleInfo}-->
                            <!--{foreach from=$saleInfo->dtl item=goods}-->
                            <tr>
                                <td>
                                    <select class="form-control" name="category" disabled="disabled">
                                        <option value="<!--{$goods->bigCategoryID}-->"><!--{$goods->bigCategoryName}--></option>
                                    </select>
                                </td>
                                <td>
                                    <select class="form-control" name="goodsCode" disabled="disabled">
                                        <option value="<!--{$goods->goodsCode}-->"><!--{$goods->goodsName}--></option>
                                    </select>
                                </td>
                                <td><input class="form-control" name="color" value="<!--{$goods->color}-->"/></td>
                                <td><input class="form-control" name="colorDis" value="<!--{$goods->colorDis}-->"/></td>
                                <td>
                                    <select class="form-control" name="packageId" disabled="disabled">
                                        <option value="<!--{$goods->packageId}-->"><!--{$goods->packageName}--></option>
                                    </select>
                                </td>
                                <td>
                                    <select class="form-control" name="specId" disabled="disabled">
                                        <option value="<!--{$goods->specId}-->"><!--{$goods->specName}--></option>
                                    </select>
                                </td>
                                <td><input class="form-control" name="amount" value="<!--{$goods->amount}-->"/></td>
                                <td <!--{if $type==1}-->style="display: none"<!--{/if}-->><input class="form-control price" name="price" id="A<!--{$goods->uuid}-->" value="<!--{$goods->price}-->"/></td>
                                <td <!--{if $type==1}-->style="display: none"<!--{/if}-->><input class="form-control beatingCost" name="beatingCost" id="B<!--{$goods->uuid}-->" value="<!--{$goods->beatingCost}-->"/></td>
                                <td style="text-align: center; line-height: 30px;<!--{if $type==1}-->display: none<!--{/if}-->" name="total"><!--{(($goods->price+$goods->beatingCost)*$goods->amount)|string_format:"%.2f"}--></td>
                                <td style="text-align: center; line-height: 30px"><input type="hidden" name="t_goodsId" value="<!--{$goods->t_goodsId}-->"/><a name="del">刪除</a><a class="save" disabled onclick="save('<!--{$goods->uuid}-->')"></a></td>
                            </tr>
                            <!--{/foreach}-->
                            <!--{else}-->
                            <tr>
                                <td><select class="form-control" name="category"></select></td>
                                <td><select class="form-control" name="goodsCode"></select></td>
                                <td><input class="form-control" name="color"/></td>
                                <td><input class="form-control" name="colorDis"/></td>
                                <td><select class="form-control" name="packageId"></select></td>
                                <td><select class="form-control" name="specId"></select></td>
                                <td><input class="form-control" name="amount"/></td>
                                <td><input class="form-control" name="price"/></td>
                                <td><input class="form-control" name="beatingCost"/></td>
                                <td style="text-align: center; line-height: 30px" name="total"></td>
                                <td style="text-align: center; line-height: 30px"><input type="hidden" name="t_goodsId"/><a name="export">导入</a>&nbsp;&nbsp;<a name="del">刪除</a></td>
                            </tr>
                            <!--{/if}-->
                        </tbody>
                    </table>
                    <div style="font-size: 14px">
                        <span name="currDate" style="float: right">日期：<!--{$currDate}--></span>
                    </div>
                    <!--{if $saleInfo->send}-->
                    <table class="table table-bordered table-hover">
                        <thead>
                        <tr><th colspan="7" style="text-align: center; display: table-cell;vertical-align: middle;"><h1>发货信息列表</h1></th></tr>
                        <tr>
                            <th width="11%" style="text-align:center;">物流单号</th>
                            <th width="4%" style="text-align:center;">运费</th>
                            <th width="9%" style="text-align:center;">时间</th>
                            <th width="10%" style="text-align:center;">商品名</th>
                            <th width="6%" style="text-align:center;">包装名称</th>
                            <th width="5%" style="text-align:center;">发货数量</th>
                            <th width="15%" style="text-align:center;">备注</th>
                        </tr>
                        </thead>
                        <!--{foreach from=$saleInfo->send item=send}-->
                        <tr>
                            <td style="text-align: center; display: table-cell;vertical-align: middle;"><!--{$send->expressNum}--></td>
                            <td style="text-align: center; display: table-cell;vertical-align: middle;"><!--{$send->freight}--></td>
                            <td style="text-align: center; display: table-cell;vertical-align: middle;"><!--{$send->createTime}--></td>
                            <td style="text-align: center; display: table-cell;vertical-align: middle;"><!--{$send->goodsName}--></td>
                            <td style="text-align: center; display: table-cell;vertical-align: middle;"><!--{$send->packageName}--></td>
                            <td style="text-align: center; display: table-cell;vertical-align: middle;"><!--{$send->amount}--></td>
                            <td style="text-align: center; display: table-cell;vertical-align: middle;"><!--{$send->expressTxt}--></td>
                        </tr>
                        <!--{/foreach}-->
                    <!--{/if}-->
                    </table>

                    <!--{if $saleInfo && $saleInfo->annex}-->
                    <!--{foreach from=$saleInfo->annex key=categoryName item=alist}-->
                    <div style="padding: 5px">
                        <div style="float: left; width: 100px"><!--{$categoryName}-->:</div>
                        <!--{foreach from=$alist item=annex}-->
                        <img src="<!--{$annex->path}-->" style="height: 200px; max-width: 400px; margin-left: 10px">
                        <!--{/foreach}-->
                    </div>
                    <!--{/foreach}-->
                    <!--{/if}-->
                </div>
            </div>
        </div>
        <div class="bottom_fixbox" style="margin-left: 0px;width: 100%;left: 0px; height: 46px;">
            <div class="tac" style="background: #dddddd;">
                <!--{if empty($saleInfo) || $saleInfo->saleStatus ==0}-->
                <input type="button" class="btn btn-primary" onclick="saveSale()" value="保存" />&nbsp;&nbsp;&nbsp;
                <!--{/if}-->
                <!--{if $saleInfo &&($saleInfo->saleStatus == 2 || $saleInfo->saleStatus == 3 || $saleInfo->saleStatus == 4) }-->
                <input type="button" class="btn btn-primary" onclick="sendSale()" value="发货" />&nbsp;&nbsp;&nbsp;
                <!--{/if}-->
                <input type="button" class="btn btn-default" onclick="E.closeWindows();" value="关闭" />
            </div>
        </div>

    </div>

</div>

<script type="text/javascript">
    var thisTr;
    var categoryData = <!--{$category}-->;
    $(function () {
        <!--{if $saleInfo && $saleInfo->saleStatus !=0}-->
        $('.comm_content').find('input,select,textarea,button,a').attr('disabled', 'disabled');
        $('#goodsList a').hide();
        $(".price").removeAttr('disabled');
        $(".beatingCost").removeAttr('disabled');
        $(".save").removeAttr('disabled');
        $(".save").html('保存');
        $(".save").show();
        <!--{/if}-->
        $('#requestDate').datepicker();

        //显示省市区
        E.region.init({
            p: 'province',
            c: 'city',
            a: 'county',
            pid: <!--{$saleInfo->province|default:0}-->,
            cid: <!--{$saleInfo->city|default:0}-->,
            ccid: <!--{$saleInfo->county|default:0}-->
        });

        //删除商品
        $('#goodsList').on('click','a[name=del]', function () {
            if($('#goodsList tr').length==1){
                E.alert('至少包含一种材料');
                return;
            }
            $(this).parents('tr').remove();
        });

        //导入商品
        $('#goodsList').on('click','a[name=export]', function () {
            var companyId = $('#companyId').val(),
                categoryId = $(this).parents('tr').find('select[name=category]').val();
            if(E.isEmpty(companyId)){
                E.alert('请选择客户信息');
                return;
            }
            if(E.isEmpty(categoryId)){
                E.alert('请选择该商品的【产品系列】');
                return;
            }
            thisTr = $(this).parents('tr');
            loadHisGoods( companyId, categoryId );
        });

        //绑定分类
        $('#goodsList select[name=category]:not(:disabled)').each(function () {
            bindCategory($(this));
            bindGoods($(this));
        });

        //切换分类
        $('#goodsList').on('change','select[name=category]', function () {
            bindGoods($(this));
        });

        //选择商品
        $('#goodsList').on('change','select[name=goodsCode]', function () {
            if($(this).val() == '') return;
            var _tr = $(this).parents('tr');
            E.ajax_post({
                url: '/invokingAjax-backend/bill/billCost.ajax?operFlg=3',
                data: {
                    goodsCode: $(this).val()
                },
                call: function( o ) {
                    if(o.code == 200){
                        //包装、规格
                        var sHtml = '', bHtml = '',
                            syncArr = o.data.syncArr,
                            birthArr = o.data.birthArr;
                        $.each(syncArr, function (i, v) {
                            sHtml += '<option value="'+ i +'">'+ v +'</option>';
                        });
                        $.each(birthArr, function (i, v) {
                            bHtml += '<option value="'+ i +'">'+ v +'</option>';
                        })
                        _tr.find("select[name=packageId]").html(sHtml);
                        _tr.find("select[name=specId]").html(bHtml);
                    }
                }
            });
        });

        //计算金额
        $('#goodsList').on('blur','input[name=price],input[name=beatingCost],input[name=amount]', function () {
            var _tr = $(this).parents('tr'),
                price = _tr.find("input[name=price]").val(),
                beatingCost = _tr.find("input[name=beatingCost]").val(),
                amount = _tr.find("input[name=amount]").val(),
                total = _tr.find("td[name=total]");
            if(price == '' || beatingCost == '' || amount == '' || !E.isNum(price) || !E.isNum(beatingCost) || !E.isNum(amount)) return;
            var t = (parseFloat(price) + parseFloat(beatingCost)) * parseFloat(amount);
            total.html(t.toFixed(2));
        });
    });

    //绑定商品数据
    function bindGoods (obj, selectCode) {
        var goodsCode = obj.parents('tr').find('select[name=goodsCode]'),
            packageId = obj.parents('tr').find('select[name=packageId]'),
            specId = obj.parents('tr').find('select[name=specId]');
        E.ajax_post({
            url: '/invokingAjax-backend/bill/billCost.ajax?operFlg=3',
            data: {
                categoryId: obj.val()
            },
            call: function( o ) {
                var html = '<option value="">请选择</option>';
                if(o.code == 200){
                    $.each(o.data, function (i, v) {
                        html += '<option value="'+ v.postID +'">'+ v.goodsName +'</option>';
                    })
                }
                goodsCode.html(html);
            }
        });
        packageId.html('<option value="">请选择</option>');
        specId.html('<option value="">请选择</option>');
    }

    //绑定分类数据
    function bindCategory (obj, selectId) {
        var html = ''; selectId = selectId || '';
        $.each(categoryData, function (i, v) {
            html += '<option value="'+ v.bigCategoryID +'" '+ (v.bigCategoryID==selectId?'selected':'') +'>'+ v.name +'</option>';
        });
        obj.append(html);
    }

    //添加商品行
    function addGoods () {
        var html = '', _tr = $('<tr></tr>');
        html += '   <td><select class="form-control" name="category"></select></td>';
        html += '   <td><select class="form-control" name="goodsCode"></select></td>';
        html += '   <td><input class="form-control" name="color"/></td>';
        html += '   <td><input class="form-control" name="colorDis"/></td>';
        html += '   <td><select class="form-control" name="packageId"></select></td>';
        html += '   <td><select class="form-control" name="specId"></select></td>';
        html += '   <td><input class="form-control" name="amount"/></td>';
        html += '   <td><input class="form-control" name="price"/></td>';
        html += '   <td><input class="form-control" name="beatingCost"/></td>';
        html += '   <td style="text-align: center; line-height: 30px" name="total"></td>';
        html += '   <td style="text-align: center; line-height: 30px"><input type="hidden" name="t_goodsId"/><a name="export">导入</a>&nbsp;&nbsp;<a name="del">刪除</a></td>';

        _tr.append(html);
        $('#goodsList').append(_tr);
        bindCategory(_tr.find('select[name=category]'));
        bindGoods(_tr.find('select[name=category]'));
    }

    //查询客户
    function searchCompany (page) {
        var company_name="";
        if($("#searchCompanyName").val()!=undefined){
            company_name = $("#searchCompanyName").val();
        }

        E.loadding.open('正在查询，请稍候...');

        E.ajax_post({
            url:  "/invokingAjax-backend/company/company.ajax?operFlg=1&company_name="+company_name,
            data: {
                sortname: 'company_id',
                sortorder: 'ASC',
                rp: 10,
                page: page,
                list_type: 'general',
                show_type: 'company',
                way_type:'searchCompany'
            },
            call: function( o ) {

                E.loadding.close();
                if (o.data) {

                    var html_str = '<table><tr><td style="width: 80px;">公司名称：</td><td><input type="text" class="form-control w300" id="searchCompanyName" name="searchCompanyName" value='+o.company_name+'></span></td><td><input type="button" value="查询" id="searchBtn" class="btn btn-primary" onclick="searchCompany(1);" /></td></tr></table>';
                    html_str += '<table class="s">';
                    html_str += '<tbody>';

                    html_str += '<tr>';
                    html_str += '<th width="80">操作</th>';
                    html_str += '<th width="*">公司名称</th>';
                    html_str += '<th width="100">联系人</th>';
                    html_str += '<th width="100">联系方式</th>';
                    html_str += '<th width="180">部门</th>';
                    html_str += '<th width="100">销售员</th>';

                    html_str += '</tr>';
                    $.each(o.data, function(k, v) {

                        if (v.mobile) {
                            var contact_way = v.mobile;
                        } else {
                            var contact_way = v.phone;
                        }

                        html_str += '<tr>';
                        html_str += '<td class="tac"><a href="javascript:void(0)" class="company_line" val0='+v.company_id+' val1='+v.company_name+' val2='+ v.connect_name+' val3='+ contact_way +' onclick="changeCompany('+v.company_id+');">选择</a></td>';
                        html_str += '<td class="tac" style="text-align:left">' + v.company_name + '</td>';
                        html_str += '<td class="tac">' + v.connect_name + '</td>';
                        html_str += '<td class="tac">' + contact_way + '</td>';
                        html_str += '<td class="tac">' + v.departmentName + '</td>';
                        html_str += '<td class="tac">' + v.userName + '</td>';
                        html_str += '</tr>';

                    });
                    html_str += '</tbody>';
                    html_str += '</table>';

                    if (o.paging) {
                        html_str += o.paging;
                    }

                    E.popup.open({
                        content: html_str,
                        title: '大客户信息列表',
                        css: 'width: 800px;'
                    });

                }else{
                    var html_str = '<table><tr><td style="width: 80px;">公司名称：</td><td><input type="text" class="form-control w300" id="searchCompanyName" name="searchCompanyName" value='+o.company_name+'></span></td><td><input type="button" value="查询" id="searchBtn" class="btn btn-primary" onclick="searchCompany(1);" /></td></tr></table>';
                    html_str += '<table class="s">';
                    html_str += '<tbody>';

                    html_str += '<tr>';
                    html_str += '<th width="80">操作</th>';
                    html_str += '<th width="*">公司名称</th>';
                    html_str += '<th width="100">联系人</th>';
                    html_str += '<th width="100">联系方式</th>';
                    html_str += '<th width="180">部门</th>';
                    html_str += '<th width="100">销售员</th>';

                    html_str += '</tr>';
                    html_str += '</tbody>';
                    html_str += '</table>';
                    E.popup.open({
                        content: html_str,
                        title: '大客户信息列表',
                        css: 'width: 800px;'
                    });
                }
            }
        });
    }

    //选择客户
    function changeCompany (searchCompanyID){
        E.ajax_post({
            url: '/invokingAjax-backend/company/company.ajax?operFlg=100&act=search',
            data: {
                company_id: searchCompanyID
            },
            call: function( o ) {
                if (o.code == 200) {
                    if ( o.data ) {
                        $.each( o.data, function( k, v ) {
                            $('#companyId').val(v.company_id);
                            $('#companyName').val(v.company_name);
                        });
                    }
                } else {
                    E.alert( o.message);
                }
                E.popup.close();
            }
        });
    }

    //保存数据
    function saveSale() {
        var saleCode = $('#saleCode').val(),
            companyId = $('#companyId').val(),
            companyName = $('#companyName').val(),
            saleName = $('#saleName').val(),
            area = $('#area').val(),
            categoryName = $('#categoryName').val(),
            expressId = $('#expressId').val(),
            request = $('#request').val(),
            requestDate = $('#requestDate').val(),
            receiver = $('#receiver').val(),
            receivMobile = $('#receivMobile').val(),
            province = $('#province').val(),
            provinceName = $('#province').find('option:selected').text(),
            city = $('#city').val(),
            cityName = $('#city').find('option:selected').text(),
            county = $('#county').val(),
            countyName = $('#county').find('option:selected').text(),
            address = $('#address').val(),
            remark = $('#remark').val();

        var error_msg = '';
        if (E.isEmpty(companyId))
            error_msg += '请选择客户<br/>';
        if (E.isEmpty(saleName))
            error_msg += '请输入项目名称<br/>';
        if (E.isEmpty(area))
            error_msg += '请输入施工面积<br/>';
        if (E.isEmpty(categoryName))
            error_msg += '请输入涂装体系<br/>';
        if (E.isEmpty(request))
            error_msg += '请填写制作内容及要求<br/>';
        if (E.isEmpty(requestDate))
            error_msg += '请选择完成时间<br/>';
        if (E.isEmpty(expressId))
            error_msg += '请选择物流方式<br/>';
        if (E.isEmpty(receiver))
            error_msg += '请填写收货人<br/>';
        if (E.isEmpty(receivMobile))
            error_msg += '请填写收货人手机<br/>';
        if (E.isEmpty(province)||E.isEmpty(city)||E.isEmpty(county)||E.isEmpty(address))
            error_msg += '请填写完整的地址信息<br/>';

        var goodsData = [], goodsTr = $('#goodsList tr');
        if (goodsTr.length == 0) {
            error_msg += '请选择材料<br/>';
        } else {
            $.each(goodsTr, function (i, v) {
                var goodsCategoryId = $(v).find('select[name=category]').val(),
                    goodsCategoryName = $(v).find('select[name=category]').find("option:selected").text(),
                    goodsCode = $(v).find('select[name=goodsCode]').val(),
                    goodsName = $(v).find('select[name=goodsCode]').find("option:selected").text(),
                    color = $(v).find('input[name=color]').val(),
                    colorDis = $(v).find('input[name=colorDis]').val(),
                    amount = $(v).find('input[name=amount]').val(),
                    packageId = $(v).find('select[name=packageId]').val(),
                    packageName = $(v).find('select[name=packageId]').find("option:selected").text(),
                    specId = $(v).find('select[name=specId]').val(),
                    specName = $(v).find('select[name=specId]').find("option:selected").text(),
                    price = $(v).find('input[name=price]').val(),
                    beatingCost = $(v).find('input[name=beatingCost]').val(),
                    t_goodsId = $(v).find('input[name=t_goodsId]').val();
                if (E.isEmpty(goodsCode)) {
                    error_msg += '请选择您要添加的材料<br/>';
                } else if (E.isEmpty(color) || E.isEmpty(colorDis) || E.isEmpty(amount) || E.isEmpty(packageId) || (E.isEmpty(price) && E.isEmpty(beatingCost))) {
                    error_msg += '请补全材料【' + goodsName + '】的数据<br/>';
                } else if ((beatingCost == 0) && (price == 0)){
                    error_msg += '单价和打浆费用至少要填写一个';
                }
                goodsData.push({
                    goodsCategoryId: goodsCategoryId,
                    goodsCategoryName: goodsCategoryName,
                    goodsCode: goodsCode,
                    goodsName: goodsName,
                    color: color,
                    colorDis: colorDis,
                    amount: amount,
                    packageId: packageId,
                    packageName: packageName,
                    specId: specId,
                    specName: specName,
                    price: price,
                    beatingCost: beatingCost,
                    t_goodsId: t_goodsId
                });
            });
        }

        if (error_msg != '') {
            E.alert(error_msg);
            return;
        }

        E.ajax_post({
            url: '/invokingAjax-backend/bill/billSale.ajax?operFlg=4',
            data: {
                saleCode: saleCode,
                companyId: companyId,
                companyName: companyName,
                saleName: saleName,
                area: area,
                categoryName: categoryName,
                expressId: expressId,
                request: request,
                requestDate: requestDate,
                receiver: receiver,
                receivMobile: receivMobile,
                province: province,
                provinceName: provinceName,
                city: city,
                cityName: cityName,
                county: county,
                countyName: countyName,
                address: address,
                remark: remark,
                dtl: goodsData
            },
            call: function (o) {
                if (o.code == 200) {
                    E.alert(o.message, 2, function () {
                        E.opener('sale.load');
                        E.closeWindows();
                    });
                } else {
                    E.alert(o.message)
                }
            }
        });
    }


    <!--{if $saleInfo}-->
    //发货
    function sendSale() {
        var html = '<div class="form-horizontal" style="position:relative;">';
        html += '<div class="form-group">';
        html += '   <label class="col-sm-3 control-label">物流单号：</label>';
        html += '   <div class="col-sm-5"><input class="form-control" name="expressNum"/></div>';
        html += '</div>';
        html += '<div class="form-group">';
        html += '   <label class="col-sm-3 control-label">运费：</label>';
        html += '   <div class="col-sm-5"><input class="form-control" name="freight"/></div>';
        html += '</div>';
        html += '<div class="form-group">';
        html += '   <label class="col-sm-3 control-label">备注：</label>';
        html += '   <div class="col-sm-7"><input class="form-control" name="expressTxt"/></div>';
        html += '</div>';

        html += '<table class="table table-bordered table-hover">';
        html += '   <thead>';
        html += '   <tr>';
        html += '       <th width="40%" style="text-align:center;">商品</th>';
        html += '       <th width="15%" style="text-align:center;">包装</th>';
        html += '       <th width="15%" style="text-align:center;">数量</th>';
        html += '       <th width="15%" style="text-align:center;">已发数量</th>';
        html += '       <th width="15%" style="text-align:center;">发货数量</th>';
        html += '   </tr>';
        html += '   </thead>';
        html += '   <tbody id="sendData">';
        <!--{foreach from=$saleInfo->dtl item=goods}-->
        html += '<tr>';
        html += '<td><!--{$goods->goodsName}--></td>';
        html += '<td style="text-align:center;"><!--{$goods->packageName}--></td>';
        html += '<td style="text-align:center;"><!--{$goods->amount}--></td>';
        html += '<td style="text-align:center;"><!--{$goods->sendAmount}--></td>';
        html += '<td><input type="hidden" name="goodsId" value="<!--{$goods->goodsId}-->" /><input type="hidden" name="recordPrice" value="<!--{$goods->price}-->"/><input type="hidden" name="recordBeatingCost" value="<!--{$goods->beatingCost}-->"/><input class="form-control" name="sendAmount" /></td>';
        html += ''
        html += '</tr>';
        <!--{/foreach}-->
        html += '</tbody>';
        html += '</table>';

        html += '</div>';

        E.popup.open({
            content: html,
            title: '发货',
            css: 'width: 600px;',
            btnName:'确认',
            isCancelBtn:false,
            btnCall: 'send()'
        });
    }

    //发货
    function send() {
        var goodsArr = [],
            expressNum = $('input[name=expressNum]').val(),
            freight = $('input[name=freight]').val(),
            expressTxt = $('input[name=expressTxt]').val(),
            tempCode = $('#saleCode').val(),
            companyId = $('#companyId').val(),
            companyName = $('#companyName').val();
        if($.trim(expressNum)==''){
            E.alert('请输入物流单号');return
        }
        if(!E.isInt(freight) || (freight < 0)){
            E.alert('请输入正确的运费');return
        }
        $msg = '';
        $.each($("#sendData").find('tr'),function () {
            var goodsId = $(this).find('input[name=goodsId]').val(),
                goodsName = $(this).find('td:eq(0)').html(),
                packageName =$(this).find('td:eq(1)').html(),
                amount = $(this).find('input[name=sendAmount]').val(),
                recordBeatingCost = $(this).find('input[name=recordBeatingCost]').val(),
                recordPrice = $(this).find('input[name=recordPrice]').val();
            if(!E.isInt(amount)){
                $msg = '发货数量只能为数字'; return false;
            }
            if(amount>0){
                goodsArr.push({goodsId:goodsId, amount: amount,recordBeatingCost: recordBeatingCost ,recordPrice: recordPrice , goodsName:goodsName,packageName:packageName});
            }
        });
        if($msg){ E.alert($msg);return }
        if(goodsArr.length==0){
            E.alert('请输入发货数量');return
        }

        E.ajax_post({
            url: '/invokingAjax-backend/bill/billSale.ajax?operFlg=2',
            data: { code: tempCode, status: 3, companyId:companyId ,companyName:companyName, expressNum: expressNum ,freight: freight , expressTxt: expressTxt, goodsArr: goodsArr},
            call: function (o) {
                if (o.code == 200) {
                    E.alert(o.message, 2, function () {
                        E.opener('sale.load');
                        E.closeWindows();
                    });
                } else {
                    E.alert(o.message)
                }
            }
        });
    }
    <!--{/if}-->

    //导入小样订单
    function exportGoods() {
        E.open("/quanliadmin/app/bill/billTemp.pscript?operFlg=1", "", {height: 620,width: 1024, top: 10, left: 150});
    }

    //查询历史小样订单数据
    function loadHisGoods(companyId, categoryId) {
        E.ajax_post({
            url: '/invokingAjax-backend/bill/billTemp.ajax?operFlg=6',
            data: { companyId: companyId, categoryId: categoryId },
            call: function (o) {
                var html_str = '';
                html_str += '<table class="s">';
                html_str += '<tbody>';
                html_str += '<tr>';
                html_str += '<th width="15%">小样订单</th>';
                html_str += '<th width="15%">项目名称</th>';
                html_str += '<th width="10%">产品系列</th>';
                html_str += '<th width="10%">颜色</th>';
                html_str += '<th width="10%">对色标准</th>';
                html_str += '<th width="10%">规格</th>';
                html_str += '<th width="12%">效果</th>';
                html_str += '<th width="8%">操作</th>';
                html_str += '</tr>';

                if (o.code == 200 && o.data) {
                        $.each(o.data, function(k, v) {
                            html_str += '<tr>';
                            html_str += '<td class="tac">' + v.tempCode + '</td>';
                            html_str += '<td class="tac">' + v.tempName + '</td>';
                            html_str += '<td class="tac">' + v.categoryName + '</td>';
                            html_str += '<td class="tac">' + v.color + '</td>';
                            html_str += '<td class="tac">' + v.colorDis + '</td>';
                            html_str += '<td class="tac">' + v.spec + '</td>';
                            html_str += '<td class="tac">' + v.effect + '</td>';
                            html_str += '<td class="tac"><a href="javascript:void(0)" class="company_line" onclick="selectHis(' + v.goodsId + ',\'' + v.color + '\',\'' + v.colorDis + '\');">选择</a></td>';
                            html_str += '</tr>';
                        });
                }
                html_str += '</tbody>';
                html_str += '</table>';
                E.popup.open({
                    content: html_str,
                    title: '导入小样商品信息',
                    css: 'width: 800px;'
                });
            }
        });
    }

    //选择商品
    function selectHis(goodsId, color, colorDis) {
        thisTr.find('input[name=t_goodsId]').val(goodsId);
        thisTr.find('input[name=color]').val(color);
        thisTr.find('input[name=colorDis]').val(colorDis);
        E.popup.close();
    }


    function save(uuid) {
        var price = $("#A"+uuid).val();
        var beatingCost = $("#B"+uuid).val();
        if (E.isEmpty(price) && E.isEmpty(beatingCost)) {
            E.alert('请补全材料的数据<br/>');
        }else if ((beatingCost == 0) && (price == 0)){
            E.alert('单价和打浆费用至少要填写一个');
        }else{
            E.ajax_post({
                url: '/invokingAjax-backend/bill/billSale.ajax?operFlg=6',
                data: {
                    uuid : uuid ,
                    price : price,
                    beatingCost : beatingCost,
                },
                call: function (o) {
                    if (o.code == 200) {
                        E.alert(o.message, 2, function () {
                            E.opener('sale.load');
                            E.closeWindows();
                        });
                    } else {
                        E.alert(o.message)
                    }
                }
            });
        }
    }
</script>

</body>
</html>